﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.DA;
using Air_condition.DO;
using ColdStorage.DOL;
using System.IO;

namespace Mainform
{
    /// <summary>
    /// Interaction logic for ucListInformation.xaml
    /// </summary>
    public partial class ucListInformation : UserControl
    {
        public ucListInformation()
        {
            InitializeComponent();
        }

        public TreeViewWithIcons tvAir;
        public TreeViewWithIcons tvCold;
        public TreeViewWithIcons tvAlt;
        public TreeViewWithIcons tvRoom;
        public int iAlternative = 1;
        public Alternative Alt;
        public Cold_Storage Cold;
        Air_condition.DO.Room airRoom;
        ColdStorage.DOL.Room coldRoom;
        public TreeViewWithIcons SelectedItem;
        TreeViewWithIcons PrivousSelectedItem;

        #region Air
        public void addAir()
        {
            tvAir = new TreeViewWithIcons();
            tvCold = null;
            Alt = new Alternative();
            trvList.Items.Clear();
            LoadLocation();
            iAlternative = 1;
            tvAir.HeaderText = "Air Condition";
            tvAir.Icon = CreateImage(Environment.CurrentDirectory + IconPath.AirIcon);            
            trvList.Items.Add(tvAir);
            ContextMenu cm = new ContextMenu();
            MenuItem AddAlternative = new MenuItem();
            AddAlternative.Header = "Add Alternative";
            AddAlternative.Click += AddAlternative_Click;
            MenuItem DeleteAlternative = new MenuItem();
            DeleteAlternative.Header = "Delete Alternative";
            DeleteAlternative.Click += DeleteAlternative_Click;
            MenuItem CopyAlternative = new MenuItem();
            CopyAlternative.Header = "Copy Alternative";
            CopyAlternative.Click += CopyAlternative_Click;
            cm.Items.Add(AddAlternative);
            cm.Items.Add(DeleteAlternative);
            cm.Items.Add(CopyAlternative);
            tvAir.ContextMenu = cm;
            addAlt(false);
            ((TreeViewItem)trvList.Items[0]).IsSelected = true;
            SelectedItem = null;
        }

        #region Alternative
        public delegate void AddAlt(Alternative alt);
        public AddAlt NewAlt;
        public void addAlt(bool copy)
        {
            tvAlt = new TreeViewWithIcons();
            tvAlt.IsExpanded = true;
            Alternative CopyAlt;
            if (copy)
            {
                CopyAlt = (Alternative)Alt.Clone();
                foreach (Air_condition.DO.Room r in CopyAlt.lRoom)
                {
                    TreeViewWithIcons tvRoom = new TreeViewWithIcons();
                    tvRoom.HeaderText = r.sName;
                    tvRoom.Tag = r;
                    tvRoom.Icon = CreateImage(Environment.CurrentDirectory + IconPath.RoomIcon);
                    tvAlt.Items.Add(tvRoom);
                }
            }
            else
            {
                CopyAlt = new Alternative();
            }
            CopyAlt.sName = "Alternative " + iAlternative;
            NewAlt(CopyAlt);
            iAlternative++;
            tvAlt.HeaderText = CopyAlt.sName;
            tvAlt.Tag = CopyAlt;
            tvAlt.Icon = CreateImage(Environment.CurrentDirectory + IconPath.AltIcon);
            tvAir.Items.Add(tvAlt);      
            tvAlt.IsSelected = true;            
        }
        #endregion

        #region Room
        public void addRoom(object o)
        {
            tvRoom = new TreeViewWithIcons();
            if (tvAir != null)
            {
                Air_condition.DO.Room r = (Air_condition.DO.Room)o;
                tvRoom.HeaderText = r.sName;
                tvRoom.Tag = r;
                tvRoom.Icon = CreateImage(Environment.CurrentDirectory + IconPath.RoomIcon);
                if (SelectedItem != null)
                {
                    if (SelectedItem.Tag is Alternative)
                    {
                        SelectedItem.Items.Add(tvRoom);
                        SelectedItem.IsExpanded = true;
                    }
                    else
                    {
                        ((TreeViewItem)SelectedItem.Parent).Items.Add(tvRoom);
                        ((TreeViewItem)SelectedItem.Parent).IsExpanded = true;
                    }
                }
                else
                {
                    ((TreeViewItem)tvAir.Items[0]).Items.Add(tvRoom);
                    ((TreeViewItem)tvAir.Items[0]).IsExpanded = true;
                }
            }
            else
            {
                ColdStorage.DOL.Room r = (ColdStorage.DOL.Room)o;
                tvRoom.HeaderText = r.sRoomName;
                tvRoom.Tag = r;
                tvRoom.Icon = CreateImage(Environment.CurrentDirectory + IconPath.RoomIcon);
                tvCold.Items.Add(tvRoom);
            }
        }

        public void deleteRoom()
        {
            ((TreeViewItem)SelectedItem.Parent).Items.Remove(SelectedItem);
        }

        public void updateRoom()
        {
            if (airRoom != null)
            {
                if (PrivousSelectedItem != null)
                {
                    if (PrivousSelectedItem.Tag is Air_condition.DO.Room)
                    {
                        PrivousSelectedItem.HeaderText = airRoom.sName;
                    }
                }
            }
        }
        #endregion
        #endregion

        #region Cold
        public delegate void AddCold(Cold_Storage cold);
        public AddCold NewCold;
        public void addColdStorage()
        {
            Cold = new Cold_Storage();
            tvCold = new TreeViewWithIcons();
            tvAir = null;
            trvList.Items.Clear();
            LoadLocation();
            tvCold.HeaderText = "Cold Storage";
            tvCold.Tag = Cold;
            tvCold.Icon = CreateImage(Environment.CurrentDirectory + IconPath.ColdIcon);
            trvList.Items.Add(tvCold);
            NewCold(Cold);
        }

        #endregion

        #region Process Tree View
        public delegate void GetRoom(object o);
        public GetRoom MyRoom;

        public delegate void GetAlt(Alternative alt);
        public GetAlt MyAlt;

        private void trvList_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (trvList.Items.Count != 0)
            {
                if (((TreeViewWithIcons)trvList.SelectedItem).Tag != null)
                {
                    if (SelectedItem != null)
                    {
                        PrivousSelectedItem = SelectedItem;
                    }
                    SelectedItem = (TreeViewWithIcons)trvList.SelectedItem;

                    if (SelectedItem.Tag is Alternative)
                    {
                        Alt = (Alternative)SelectedItem.Tag;

                        if (MyAlt != null)
                        {
                            MyAlt(Alt);
                        }
                    }
                    else
                    {
                        if (SelectedItem.Tag is Air_condition.DO.Room)
                        {
                            Alt = (Alternative)((TreeViewWithIcons)SelectedItem.Parent).Tag;
                            if (MyAlt != null)
                            {
                                MyAlt(Alt);
                            }
                            airRoom = (Air_condition.DO.Room)SelectedItem.Tag;
                            if (MyRoom != null)
                            {
                                MyRoom(airRoom);
                            }
                        }
                        else
                        {
                            if (SelectedItem.Tag is ColdStorage.DOL.Room)
                            {
                                coldRoom = (ColdStorage.DOL.Room)SelectedItem.Tag;
                                if (MyRoom != null)
                                {
                                    MyRoom(coldRoom);
                                }
                            }
                            else
                            {
                                NewCold(Cold);
                            }
                        }
                    }
                }
                else
                {
                    if (SelectedItem == null)
                    {
                        Alt = (Alternative)((TreeViewWithIcons)tvAir.Items[0]).Tag;
                    }
                    if (MyAlt != null)
                    {
                        MyAlt(Alt);
                    }
                }
                if (SelectedItem != null)
                {
                    PrivousSelectedItem = SelectedItem;
                }
                if (tvAir != null)
                {
                    txtProject.Text = Alt.sProject;
                    txtBuilding.Text = Alt.sBuildingowner;
                    txtDescription.Text = Alt.sDescription;
                    cbxLocation.SelectedIndex = Alt.iLocation;
                }
                else
                {
                    txtProject.Text = Cold.sProjectName;
                    txtBuilding.Text = Cold.sProjectOwner;
                    txtDescription.Text = Cold.sDescription;
                    cbxLocation.SelectedIndex = Cold.iLocation;
                }
            }
        }

        private void trvList_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (tvAir != null)
            {
                if (((TreeViewWithIcons)trvList.SelectedItem).Tag is Alternative)
                {
                    if (Alt.sName == "Alternative 1")
                    {
                        ((MenuItem)tvAir.ContextMenu.Items[1]).IsEnabled = false;
                    }
                    else
                    {
                        ((MenuItem)tvAir.ContextMenu.Items[1]).IsEnabled = true;
                    }
                    tvAir.ContextMenu.Visibility = Visibility.Visible;
                    tvAir.ContextMenu.IsOpen = true;
                }
                else
                {
                    tvAir.ContextMenu.Visibility = Visibility.Hidden;
                }
            }
        }

        private void AddAlternative_Click(object sender, RoutedEventArgs e)
        {
            addAlt(false);
        }

        public delegate void DeleteAlt();
        public DeleteAlt dAlt;
        private void DeleteAlternative_Click(object sender, RoutedEventArgs e)
        {
            tvAir.Items.Remove(SelectedItem);
            dAlt();
        }

        private void CopyAlternative_Click(object sender, RoutedEventArgs e)
        {
            addAlt(true);
        }

        #region Process Location
        public void LoadLocation()
        {
            cbxLocation.ItemsSource = null;
            cbxLocation.Items.Clear();
            cbxLocation.ItemsSource = LibraryManagerInCom.LocationList;
            cbxLocation.DisplayMemberPath = "location";
            cbxLocation.SelectedIndex = 0;
        }

        public delegate void GetLocation(string sOutsideTemperature);
        public GetLocation MyLocation;

        private void cbxLocation_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxLocation.Items.Count != 0)
            {
                string sOutsideTemperature = (((tb_location)cbxLocation.SelectedItem).average_temperature + 0.125 * ((tb_location)cbxLocation.SelectedItem).max_temperature).ToString();
                MyLocation(sOutsideTemperature);
            }
            cbxApply.IsEnabled = true;
        }

        private void txtProject_TextChanged(object sender, TextChangedEventArgs e)
        {
            cbxApply.IsEnabled = true;
        }

        private void txtBuilding_TextChanged(object sender, TextChangedEventArgs e)
        {
            cbxApply.IsEnabled = true;
        }

        private void txtDescription_TextChanged(object sender, TextChangedEventArgs e)
        {
            cbxApply.IsEnabled = true;
        }

        private void cbxApply_Click(object sender, RoutedEventArgs e)
        {
            cbxApply.IsEnabled = false;
            if (tvAir != null)
            {
                Alt.sProject = txtProject.Text;
                Alt.sBuildingowner = txtBuilding.Text;
                Alt.sDescription = txtDescription.Text;
                Alt.iLocation = cbxLocation.SelectedIndex;
                Alt.sLocation = cbxLocation.Text;
            }
            else
            {
                Cold.sProjectName = txtProject.Text;
                Cold.sProjectOwner = txtBuilding.Text;
                Cold.sDescription = txtDescription.Text;
                Cold.iLocation = cbxLocation.SelectedIndex;
                Cold.sLocation = cbxLocation.Text;
            }
        }
        #endregion

        #endregion

        /// <summary>
        /// This Class Provides the TreeView with extended functionalities like,
        /// Adding the HeaderText feature to Node, Setting the icon for TreeViewNode.
        /// </summary>
        public class TreeViewWithIcons : TreeViewItem
        {
            #region Global variables
            ImageSource iconSource;
            TextBlock textBlock;
            Image icon;
            #endregion Global variables

            #region Constructors and Destructors

            public TreeViewWithIcons()
            {
                StackPanel stack = new StackPanel();
                stack.Orientation = Orientation.Horizontal;
                Header = stack;
                //Uncomment this code If you want to add an Image after the Node-HeaderText
                //textBlock = new TextBlock();
                //textBlock.VerticalAlignment = VerticalAlignment.Center;
                //stack.Children.Add(textBlock);
                icon = new Image();
                icon.VerticalAlignment = VerticalAlignment.Center;
                icon.Margin = new Thickness(0, 0, 4, 0);
                icon.Source = iconSource;
                stack.Children.Add(icon);
                //Add the HeaderText After Adding the icon
                textBlock = new TextBlock();
                textBlock.VerticalAlignment = VerticalAlignment.Center;
                stack.Children.Add(textBlock);

            }

            #endregion Constructors and Destructors

            #region Properties

            /// <summary>
            /// Gets/Sets the Selected Image for a TreeViewNode
            /// </summary>
            public ImageSource Icon
            {
                set
                {
                    iconSource = value;
                    icon.Source = iconSource;
                }

                get
                {
                    return iconSource;
                }

            }

            #endregion Properties

            #region Event Handlers

            /// <summary>
            /// Event Handler on UnSelected Event
            /// </summary>
            /// <param name="args">Eventargs</param>
            protected override void OnUnselected(RoutedEventArgs args)
            {
                base.OnUnselected(args);
                icon.Source = iconSource;

            }
            /// <summary>
            /// Event Handler on Selected Event 
            /// </summary>
            /// <param name="args">Eventargs</param>
            protected override void OnSelected(RoutedEventArgs args)
            {
                base.OnSelected(args);
                icon.Source = iconSource;
            }

            /// <summary>
            /// Gets/Sets the HeaderText of TreeViewWithIcons
            /// </summary>
            public string HeaderText
            {
                set
                {
                    textBlock.Text = value;
                }
                get
                {
                    return textBlock.Text;
                }
            }


            #endregion Event Handlers

        }

        public BitmapImage CreateImage(string path)
        {
            BitmapImage myBitmapImage = new BitmapImage();
            myBitmapImage.BeginInit();
            myBitmapImage.UriSource = new Uri(path);
            myBitmapImage.EndInit();
            return myBitmapImage;
        }

        public void LoadAlternative(Alternative alt)
        {
            if (alt.sName != "Alternative 1")
            {
                Alt = (Alternative)alt.Clone();
                addAlt(true);
            }
            else {                
                ((TreeViewWithIcons)tvAir.Items[0]).Tag = (Alternative)alt.Clone();                
            }
        }

    }
}
